//
//  BagListViewController.m
//  Emoticon
//
//  Created by ytkjs2 on 2020/4/10.
//  Copyright © 2020 ytk. All rights reserved.
//

#import "BagListViewController.h"
#import "ListViewCell.h"
#import "SearchDownloadController.h"

@interface BagListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)NSMutableArray * dataSource;

@property (nonatomic, assign)NSInteger pageIndex;

@end

@implementation BagListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.collect registerNib:[UINib nibWithNibName:@"ListViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ListViewCell"];
    
    __weak typeof(self)weakSelf = self;
    
    _collect.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pageIndex = 0;
        weakSelf.dataSource = [NSMutableArray array];
        
        [PictureData getRequestWithServiceUrl:[NSString stringWithFormat:@"%@%@?",SEARCH_BAG2_URL,weakSelf._id] parameters:@{@"limit":@(64),@"skip":@(weakSelf.pageIndex),@"order":@"new"} success:^(id  _Nonnull responseObject) {
            
            NSArray * arr = [NSArray arrayWithArray:[[responseObject objectForKey:@"res"]objectForKey:@"data"]];
            
            for (NSDictionary * dic in arr) {
                SearchBagListModel * model = [SearchBagListModel mj_objectWithKeyValues:dic];
                [weakSelf.dataSource addObject:model];
            }
            [weakSelf.collect reloadData];
            [weakSelf.collect.mj_header endRefreshing];
            
        } failure:^(NSError * _Nonnull error) {
            [weakSelf.collect.mj_header endRefreshing];
        }];
    }];
    [_collect.mj_header beginRefreshing];
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ListViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListViewCell" forIndexPath:indexPath];
    
    SearchBagListModel * model = _dataSource[indexPath.item];
    
    NSString * imgUrl = [model.small_url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [cell.IMG sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"icon_placeholderImage"]];
    
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 6.5;//上下行
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 6.5;//行距
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(6.5, 12, 6.4, 12 );//分别为上、左、下、右
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat width = (rScreenWidth - 61)/4;
    
    return CGSizeMake(width,width);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SearchDownloadController * downloadVC = [[SearchDownloadController alloc]initWithNibName:@"SearchDownloadController" bundle:nil];
    downloadVC.dataSource = [_dataSource mutableCopy];
    downloadVC.picIndex = indexPath.item;
    downloadVC.isBag = YES;
    [self.navigationController pushViewController:downloadVC animated:YES];
}

@end
